package com.atguigu.boot;

import ch.qos.logback.core.db.DBHelper;
import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import com.atguigu.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication//这是一个SpringBoot的应用
//这个是三个主要注解集合@SpringBootConfiguration  @EnableAutoConfiguration  @ComponentScan
public class MainApplication {

    public static void main(String[] args) {
        //1.返回我的IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
        //2.查看容器里面的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        /*
        //3.从容器中获取组件
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);
        //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。
        //SpringBoot总会检查这个组件是否在容器中有。  保持组件单实例
        Pet tom01 = run.getBean("tom", Pet.class);
        Pet tom02 = run.getBean("tom", Pet.class);
        User user01 = run.getBean("user01", User.class);
        User user02 = run.getBean("user01", User.class);
        System.out.println("组件：" + (tom01 == tom02));//true 获取的都是之前注册容器中的单实例对象
        System.out.println("组件：" + (user01 == user02));//true 获取的都是之前注册容器中的单实例对象

        User user03 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);
        System.out.println("用户的宠物："+(user03.getPet() == tom));
        System.out.println("============================");
        //4.获取组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        for (String s : beanNamesForType) {
            System.out.println(s);
        }
        DBHelper bean1 = run.getBean(DBHelper.class);
        System.out.println(bean1);
        */

        //5.判断容器中组件的存在
        //利用ConditionalOnMissingBean条件装配 注入的组件
        boolean tom = run.containsBean("tom");
        System.out.println("容器中Tom组件："+tom);//true
        boolean user01 = run.containsBean("user01");
        System.out.println("容器中user01组件："+user01);//true
        //利用@ImportResource导入配置文件 成功注入的组件
        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("haha："+haha);//true
        System.out.println("hehe："+hehe);//true
    }
}
